package top.mcwebsite.playandroid.business.me.impl.coin_rank.ui

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.ArrowBack
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.LoadState
import androidx.paging.compose.collectAsLazyPagingItems
import top.mcwebsite.lib.route.api.EasyRouter
import top.mcwebsite.lib.router.annotation.Route
import top.mcwebsite.playandroid.business.me.impl.coin.ui.CoinFetchItemSkeleton
import top.mcwebsite.playandroid.business.me.impl.coin_rank.vm.CoinRankIntent
import top.mcwebsite.playandroid.business.me.impl.coin_rank.vm.CoinRankUiEffect
import top.mcwebsite.playandroid.business.me.impl.coin_rank.vm.CoinRankViewModel
import top.mcwebsite.playandroid.business.me.impl.model.CoinRankItemModel
import top.mcwebsite.playandroid.common.constant.RouteConfig
import top.mcwebsite.playandroid.strings.Str

@Route(RouteConfig.CoinRank.PATH)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CoinRankPage() {
    val viewModel: CoinRankViewModel = viewModel()
    viewModel.HandleUiEffect {
        when (it) {
            is CoinRankUiEffect.NavigationBackUiEffect -> {
                EasyRouter.popBackStack()
            }
        }
    }
    Scaffold(
        topBar = {
            TopAppBar(
                navigationIcon = {
                    IconButton(onClick = { viewModel.sendIntent(CoinRankIntent.ClickBackIntent) }) {
                        Icon(imageVector = Icons.Rounded.ArrowBack, contentDescription = stringResource(id = Str.back))
                    }
                },
                title = {
                    Text(text = stringResource(id = Str.coin_rank))
                })
        }
    ) {
        val lazyPagingItems = viewModel.pagerFlow.collectAsLazyPagingItems()
        LazyColumn(
            modifier = Modifier.padding(it)
        ) {
            if (lazyPagingItems.loadState.refresh == LoadState.Loading) {
                item {
                    Column(
                        modifier = Modifier.fillMaxHeight()
                    ) {
                        repeat(9) {
                            CoinFetchItemSkeleton()
                        }
                    }
                }
            }

            items(count = lazyPagingItems.itemCount) { index ->
                lazyPagingItems[index]?.let { coinFetchItemModel ->
                    CoinRankItem(index, coinRankItemModel = coinFetchItemModel)
                }
            }



            if (lazyPagingItems.loadState.append == LoadState.Loading) {
                item {
                    CircularProgressIndicator(
                        modifier = Modifier
                            .fillMaxWidth()
                            .wrapContentWidth(Alignment.CenterHorizontally)
                    )
                }
            }
        }
    }
}

@Composable
private fun CoinRankItem(index: Int, coinRankItemModel: CoinRankItemModel) {
    Box {
        Row(
            modifier = Modifier.padding(horizontal = 12.dp, vertical = 10.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(text = (index + 1).toString())
            Text(text = coinRankItemModel.userName, modifier = Modifier.padding(start = 5.dp))
            Spacer(modifier = Modifier.weight(1f))
            Text(text = coinRankItemModel.coinCount.toString(), color = MaterialTheme.colorScheme.primary)
        }
    }
}